<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of addressbook
 *
 * @author hassanali
 */
class addressbook extends cms_controller {

    //put your code here
    public function __construct() {
        parent::__construct();
        $this->load->model('address_model');
    }

    public function index() {
        $data['head_title'] = lang('label_menu_link_addressbooks');
        $data['result'] = $da = $this->address_model->get(array('addressbook.user_id' => $this->user_id));
        $data['total'] = count($da);
        $this->themes->render('address/index', $data);
    }
    
    public function add() {
        $data = $this->address_model->get(array('addressbook.user_id' => $this->user_id));
        if(count($data) >= 7)
            $this->themes->set_message(array('url' => 'addressbook', 'type' => 'error', 'message' => 'message_address_max_6'));
           if ($this->input->post()) {
                $this->session->set_userdata('post', $this->input->post());
                $this->form_validation->set_rules('name', lang('label_name'), 'required|trim');
                $this->form_validation->set_rules('mobile', lang('label_mobile'), 'required|trim');
                $this->form_validation->set_rules('city', lang('label_city'), 'required|trim');
                $this->form_validation->set_rules('address', lang('label_address'), 'required|trim|min_length[6]');
                if ($this->form_validation->run()) {
                    $city = $this->input->post('city');
                    $mobile = $this->input->post('mobile');
                    $name = $this->input->post('name');
                    if ($this->input->post('default')) {
                        $default = 'YES';
                        $this->address_model->nodefault($this->user_id);
                    } else {
                        $default = 'NO';
                    }
                    $user_data['address'] = $this->input->post('address');
                    $user_data['default'] = $default;
                    $user_data['mobile'] = $mobile;
                    $user_data['cities_id'] = $city;
                    $user_data['name'] = $name;
                    $user_data['user_id'] = $this->user_id;
                    
                    if ($this->address_model->save($user_data)) {
                        $this->session->unset_userdata('post');
                        $this->themes->set_message(array('url' => 'addressbook', 'type' => 'success', 'message' => 'message_edit_success'));
                    } else {
                        get_save_error();
                    }
                } else {
                    get_validation_errors();
                }
            }

            $this->load->model('cities_model', 'cities');
            $this->pass['cities'] = $this->cities->get();
            $this->themes->render('address/add', $this->pass);
        
    }
    
    public function edit($id = false) {
        if ($id) {
            $this->pass['addressbook'] = $id_id = $this->address_model->is_found(array('addressbook.id' => $id));
            if ($this->input->post()) {
                $this->session->set_userdata('post', $this->input->post());
                $this->form_validation->set_rules('name', lang('label_name'), 'required|trim');
                $this->form_validation->set_rules('mobile', lang('label_mobile'), 'required|trim');
                $this->form_validation->set_rules('city', lang('label_city'), 'required|trim');
                $this->form_validation->set_rules('address', lang('label_address'), 'required|trim|min_length[6]');
                if ($this->form_validation->run()) {
                    $city = $this->input->post('city');
                    $mobile = $this->input->post('mobile');
                    $name = $this->input->post('name');
                    if ($this->input->post('default')) {
                        $default = 'YES';
                        $this->address_model->nodefault($this->user_id);
                    } else {
                        $default = 'NO';
                    }
                    $user_data['address'] = $this->input->post('address');
                    $user_data['default'] = $default;
                    $user_data['mobile'] = $mobile;
                    $user_data['cities_id'] = $city;
                    $user_data['user_id'] = $this->user_id;
                    $user_data['name'] = $name;
                    if ($this->address_model->save($user_data, array('id' => $id), TRUE)) {
                        $this->session->unset_userdata('post');
                        $this->themes->set_message(array('url' => 'addressbook', 'type' => 'success', 'message' => 'message_edit_success'));
                    } else {
                        get_save_error();
                    }
                } else {
                    get_validation_errors();
                }
            }

            $this->load->model('admin/cities_model', 'cities');
            $this->pass['cities'] = $this->cities->get();
            $this->themes->render('address/edit', $this->pass);
        } else {
            show_404();
        }
    }
    
    public function delete($id = false) {
        if ($id) {
            $ref = $this->agent->referrer();
            $falid_id = $this->address_model->is_found(array('addressbook.id' => $id), array(), 'row', $ref);
            if ($falid_id->default == 'YES'){
                $this->themes->set_message(array('url' => $ref, 'type' => 'error', 'message' => 'message_can_not_delete_is_default'));
            }
            if ($this->address_model->delete(array('id' => $id))) {
                $this->themes->set_message(array('url' => $ref, 'type' => 'success', 'message' => 'message_delete_success'));
            } else {
                $this->themes->set_message(array('url' => $ref, 'type' => 'error', 'message' => 'message_delete_error'));
            }
        } else {
            show_404();
        }
    }

}

?>
